package edu.base.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import edu.base.model.CatDetail;
import edu.base.model.Category;
import edu.base.model.MaintenanceRecord;
import edu.base.model.MaintenanceRecordJoinUsers;
import edu.base.service.CatDetailService;
import edu.base.service.CategoryService;
import edu.base.service.MaintenanceRecordService;
import edu.base.service.UsersService;
import edu.base.util.PageUtil;

@RequestMapping("/kefu")
@Controller
public class KefuController {
	
	final static org.slf4j.Logger log = LoggerFactory.getLogger(KefuController.class);

	@Autowired
	UsersService usersService; 
	@Autowired
	CategoryService categoryService;
	@Autowired
	CatDetailService catDetailService;
	@Autowired
	MaintenanceRecordService maintenanceRecordService;
	
	@RequestMapping("index")
	public String index() {
		//return "/kefu/index";
		return "kefu/public/kefu_pub_aside";
	}

	@RequestMapping("welcome")
	public String welcome() {
		return "/admin/welcome";
	}
	
	
	@RequestMapping("record/lst")
	public String recordLst(Integer p,String startDate, String endDate,Integer state,Integer workerId,Integer customerId,Model model) throws ParseException {
		Date date1=null;
		Date date2 = null;
		if(startDate != null) {
			date1 = new SimpleDateFormat("yyyy-MM-dd").parse(startDate);
		}
		if(endDate != null) {
			date2 = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);
		}
		if(p == null) {
			p = 1;
		}
		
		List<MaintenanceRecordJoinUsers> list = maintenanceRecordService.query(date1, date2, customerId, workerId, state, p, 5);
		Integer count = maintenanceRecordService.count(date1, date2, customerId, workerId, state);
		String url = "/kefu/record/lst?";
		if(startDate != null) {
			url += "&startDate=" + startDate;
		}
		if(endDate != null) {
			url += "&endDate=" + endDate;
		}
		if(state != null) {
			url += "&state=" + state;
		}
		if(customerId != null) {
			url += "&customerId=" + customerId;
		}
		if(workerId != null) {
			url += "&workerId=" + workerId;
		}
		PageUtil page = new PageUtil(5, count, p, url);
		model.addAttribute("page", page.page());
		model.addAttribute("list", list);
		model.addAttribute("p", p);
		model.addAttribute("startDate",startDate);
		model.addAttribute("endDate", endDate);
		model.addAttribute("state", state);
		model.addAttribute("customerId", customerId);
		model.addAttribute("workerId", workerId);
		model.addAttribute("page", page.page());
		return "/kefu/record/lst";
	}
	
	
	@RequestMapping("record/add")
	public String RecordAdd() {
		return "/kefu/record/add";
	}
	
	
	@RequestMapping("record/doAdd")
	public String RecordDoAdd(MaintenanceRecord item,Model model) {
		item.setStartDate(new Date());
		maintenanceRecordService.insert(item);
		return "redirect:/kefu/record/lst";
	}
	
	@RequestMapping("record/edit")
	public String RecordEdit(Integer id,Model model) {
		MaintenanceRecord item = maintenanceRecordService.findByKey(id);
		model.addAttribute("item", item);
		return "/kefu/record/edit";
	}
	
	@RequestMapping("record/update")
	public String recordUpdate(MaintenanceRecord item) {
		maintenanceRecordService.update(item);
		//return "redirect:/kefu/record/edit?id=" + item.getRecordId();
		return "redirect:/kefu/record/lst";
	}
	
	
	@RequestMapping("record/detail")
	public String RecordDetail(Integer id,HttpSession session,Model model) {
		MaintenanceRecord item = maintenanceRecordService.findByKey(id);
		List<CatDetail> lst = catDetailService.find(null, null, false, id);
		model.addAttribute("lst", lst);
		model.addAttribute("item", item);
		return "/kefu/record/detail";
	}
	
	
	@RequestMapping("record/pay")
	public String recordPay(Integer id) {
		MaintenanceRecord item = new MaintenanceRecord();
		item.setRecordId(id);
		item.setState(5);
		maintenanceRecordService.update(item);
		return "redirect:/kefu/record/detail?id=" + id;
	}
	
	@RequestMapping("record/del")
	public String recordDel(Integer id) {
		maintenanceRecordService.del(id);
		return "redirect:/kefu/record/lst";
	}
	
}
